07. What’s the Damage, Captain?

Prepare

For the final exercise of this session, we are going to test your ability to combine values and operators to calculate the final score when the game is over. Currently, when a match completes, you see the following output in Xcode:

The Debug Area shows a final score of 0. That's not right!

The Debug Area shows a final score of 0. That's not right!

Learn

The final score should be calculated using the following formula:

  • finalScore = (enemyShipsSunk * sinkBonus) + (humanShipsRemaining * shipBonus) - (numberOfGuesses * guessPenalty)

Note: The numberOfGuesses should include guesses resulting in a hit and guesses resulting in a miss.

You will perform the necessary calculations within calculateFinalScore in the ControlCenter.swift file. The values you will need are contained in an object called gameStats—which is an instance of the GameStats struct. This struct purposefully contains values that are not exactly the ones in the formula. It's not a mistake. You have to perform some extra calculations to get the final score.

The GameStats struct contains values useful for calculating the final score.

The GameStats struct contains values useful for calculating the final score.

Use the values in gameStats to create a variable called finalScore with the correct value according to the formula above. Then, the statement return finalScore (don't modify this line of code) will send your value to the part of the system that displays the final score at the end of a game. If at any point in your calculation, you would like to see the current value of a variable, use a print statement like the following:

print("the value of final score is \(finalScore)")

And, then you will see the results of any print statements in the Debug area of Xcode.

Don't see the expected print statements? Our coaches can help debug! Reach out to them in the forums.

QUESTION:

Nice work! If this was a production-ready app, then we could add a feature that graphically displays the finalScore to the user or even posts it to a leaderboard in the Apple Game Center. Besides these, what other features would you add?

ANSWER:

Thanks for your response! Consider sharing your ideas with your fellow classmates on the forums.